package com.lyd.management.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.util.SaResult;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyd.management.mapper.RoomMapper;
import com.lyd.management.pojo.Food;
import com.lyd.management.pojo.Room;
import com.lyd.management.pojo.User;
import com.lyd.management.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Wrapper;
import java.util.List;

@RestController
@RequestMapping("/room")
public class RoomController {

    @Autowired
    private RoomService roomService;

    //增加房间
    @SaCheckLogin
    @SaCheckRole("admin")
    @PostMapping("/add")
    public SaResult add(@RequestBody Room room){
       if(roomService.save(room)) {
           return SaResult.ok("新增成功");
       }else{
           return SaResult.error("新增失败");
       }
    }


    //删除客房
    @SaCheckLogin
    @SaCheckRole("admin")
    @PostMapping("/delete")
    public SaResult delete(@RequestBody JSONObject object){
        String id =  object.getString("id");
        if(roomService.removeById(id)){
            return SaResult.ok("删除成功");
        }else{
            return SaResult.error("删除失败");
        }
    }

    //修改客房
    @SaCheckLogin
    @SaCheckRole("admin")
    @PostMapping("/modify")
    public SaResult modify(@RequestBody Room room){
        if(roomService.updateById(room)){
            return SaResult.ok("修改成功");
        }else{
            return SaResult.error("修改失败");
        }
    }

    //查询客房
    @SaCheckLogin
    @PostMapping("/select")
    public SaResult selectById(@RequestBody JSONObject object){
       String id =  object.getString("id");
        Room room  = roomService.getById(id);
        return SaResult.data(room);
    }

    //预订客房
    @SaCheckLogin
    @PostMapping("/book")
    public SaResult Book(@RequestBody Room room){
        //找到此房间
        Room room1 = roomService.getById(room.getRoomId());
        if("noBook".equals(room1.getRoomStatus())){
            room1.setRoomStatus("Book");
            return SaResult.ok("预定成功");
        }else{
            return SaResult.error("预定失败");
        }


    }


    //分页食物列表
    @SaIgnore
    @SaCheckLogin
    @GetMapping("/list")
    public SaResult getList(int page, int pageSize){
        //构造分页构造器
        Page<Room> pageInfo = new Page<>(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Room> queryWrapper = new LambdaQueryWrapper<>();
//        //添加排序条件
//        queryWrapper.orderByDesc(Food::getUpdateTime);
        //执行查询
        roomService.page(pageInfo, queryWrapper);
        return SaResult.data(pageInfo);
    }

//    @SaCheckLogin
//    @PostMapping("/list")
//    public SaResult List(@RequestBody Room room){
//        LambdaQueryWrapper<Room> queryWrapper = new LambdaQueryWrapper<>();
//        //如果传入了类型，就返回该类型的房间
//        if(room.getRoomType() != null && !room.getRoomType().equals("")){
//           queryWrapper.eq(Room::getRoomType,room.getRoomType());
//        }
//        List<Room> roomList = roomService.list(queryWrapper);
//        return SaResult.data(roomList);
//    }

    @SaCheckLogin
    @PostMapping("/toliet")
    public SaResult toliet(@RequestBody Room room){
        Room room1 = roomService.getById(room.getRoomId());
        room1.setToiletLight(true);
        return SaResult.ok("已打开");
    }








}
